package com.digiwin.escloud.test.core.parser.xml;

import com.digiwin.escloud.test.core.entity.TestCaseEntity;
import com.digiwin.escloud.test.core.parser.TestDataParser;
import com.digiwin.escloud.test.core.parser.xml.dom4j.Dom4JReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.*;

/**
 * @Author yanggld
 * @Date 2020/5/7-14:48
 * 默认解析xml文件生成TestCaseEntity
 */
@Slf4j
public class XmlDataParser implements TestDataParser {
	@Override
	public List<TestCaseEntity> parse(String config) {
		try {
			Document document = Dom4JReader.getDocument(config);
			Element rootElement = document.getRootElement();
			List<TestCaseEntity>  list = parseRoot(rootElement);
			return list;
		} catch (Exception e) {
			log.error("Parse config failed", e);
		}
		return null;
	}

	private List<TestCaseEntity>  parseRoot(Element element) {
		List<TestCaseEntity> testCaseEntityList = new ArrayList<>();
		for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
			Object childElementObject = elementIterator.next();
			if (childElementObject instanceof Element) {
				TestCaseEntity testCaseEntity = new TestCaseEntity();
				Element childElement = (Element) childElementObject;
				if (StringUtils.equals(childElement.getName(), "case")) {
					parseCase(childElement, testCaseEntity);
					if (Objects.nonNull(testCaseEntity)) {
						testCaseEntityList.add(testCaseEntity);
					}
				}
			}
		}
		return testCaseEntityList;
	}

	private void parseCase(Element element, TestCaseEntity testCaseEntity){
		for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
			Object childElementObject = elementIterator.next();
			if (childElementObject instanceof Element) {
				Element childElement = (Element) childElementObject;
				if (StringUtils.equals(childElement.getName(), "name")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setName(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "requestBody")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setRequestBody(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "url")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setUrl(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "requestMethod")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setRequestMethod(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "requestParam")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setRequestParam(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "pathVariable")) {
					String nameValue = childElement.getStringValue();
					testCaseEntity.setPathVariable(nameValue);
				} else if (StringUtils.equals(childElement.getName(), "multiPart")) {
					Attribute serviceNameAttribute = childElement.attribute("name");
					if (serviceNameAttribute == null) {
//						throw new DiscoveryException("Attribute[" + ConfigConstant.SERVICE_NAME_ATTRIBUTE_NAME + "] in element[" + childElement.getName() + "] is missing");
					}
					String serviceName = serviceNameAttribute.getData().toString().trim();
					String nameValue = childElement.getStringValue();
					Map<String,String> map = new HashMap<>();
					map.put(serviceName,nameValue);
					testCaseEntity.setMultiPart(map);
				}
			}
		}
	}

}
